<?php

namespace App\Service;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use GuzzleHttp\Client;
use SebastianBergmann\CodeCoverage\Report\PHP;

class MchantService
{
    public function get_mchant_user($data){
        print_r($data);
        # 查询用户ID
        $where = [
            "mobile"=>$data["mobile"],
            "password"=>md5($data["password"]),
        ];
        print_r($where);
        $mchant_info = Db::table("mchant_user")->where($where)->select("id","mobile","username","is_deleted","status","create_at")->first();
        if ($mchant_info){
            if ($mchant_info->is_deleted ==1 or $mchant_info->status == 0){
                return [
                    "code"=>20061,
                    "message"=>"该用户已被禁用！",
                    "content"=>(object)[]
                ];
            }else{
                Db::table("mchant_user")->where(["id"=>$mchant_info->id])->update(["login_at"=>date("Y-m-d H:i:s")]);
                return [
                    "code"=>10000,
                    "message"=>"登陆成功",
                    "content"=>$mchant_info
                ];
            }
        }else{
            return [
                "code"=>20060,
                "message"=>"账号密码错误",
                "content"=>(object)[]
            ];
        }

    }

    public function get_order_info($order_no){
        # 查询的订单详情
        $where = [
            "order_no"=>$order_no,
        ];
        $order_info = Db::table("shop_order")->where($where)->select(
            "id","order_no","uuid","amount_real","amount_total","number_goods","order_remark",
            "cancel_status","status","cancel_datetime","create_at","goods_id","goods_name","goods_cover","goods_suit_store",
            "goods_shuoming","user_cover","user_name","user_phone","integral","effective_time")->first();
        if ($order_info){
            if ($order_info->cancel_status!=0 or $order_info->status!=4){
                return [
                    "code"=>20063,
                    "message"=>"卡劵不存在!",
                    "content"=>(object)[]
                ];
            }else{
                return [
                    "code"=>10000,
                    "message"=>"成功",
                    "content"=>$order_info
                ];
            }
        }else{
            return [
                "code"=>20062,
                "message"=>"卡劵不存在！",
                "content"=>(object)[]
            ];
        }
    }

    public function get_order_list($mchant_id,$page=1){
        # 查询的订单详情
        $where = [
            ['mchant_id', '=', $mchant_id],
            ['cancel_datetime', '>=', date("Y-m-d")." 00:00:00"],
            ['cancel_datetime', '<=', date("Y-m-d")." 23:59:59"],
        ];
        $order_list = Db::table("shop_order")->where($where)->select(
            "id","order_no","uuid","amount_real","amount_total","number_goods","order_remark",
            "cancel_status","status","cancel_datetime","create_at","goods_id","goods_name","goods_cover","goods_suit_store",
            "goods_shuoming","user_cover","user_name","user_phone","integral","effective_time","order_remark")->orderBy("cancel_datetime","desc")->forPage($page)->get();
        $order_amount_total = Db::table("shop_order")->where($where)->sum("amount_total");
        if ($order_list){
            return [
                "code"=>10000,
                "message"=>"成功",
                "content"=>[
                    "order_amount_total"=>$order_amount_total,
                    "list"=>$order_list,
                ],
            ];
        }else{
            return [
                "code"=>10000,
                "message"=>"成功",
                "content"=>[
                    "order_amount_total"=>0,
                    "list"=>[],
                ]
            ];
        }
    }

    public function cancel($mchant_id,$order_no){
        # 查询用户状态
        $mchant_info = Db::table("mchant_user")->where(["id"=>$mchant_id])->first();
        if ($mchant_info){
            if ($mchant_info->is_deleted ==1 or $mchant_info->status == 0){
                return [
                    "code"=>20061,
                    "message"=>"该用户已被禁用",
                    "content"=>(object)[]
                ];
            }else{
                # 查询订单状态
                $order_info = Db::table("shop_order")->where([
                    "order_no"=>$order_no
                ])->select("id","status","cancel_status","effective_time")->first();
                if ($order_info){
                    if ($order_info->status!=4){
                        return [
                            "code"=>20063,
                            "message"=>"卡劵不存在",
                            "content"=>(object)[]
                        ];
                    }
                }else{
                    return [
                        "code"=>20063,
                        "message"=>"卡劵不存在",
                        "content"=>(object)[]
                    ];
                }
                if ($order_info->cancel_status==0){
                    $effective_time_status = 1;
                    if ($order_info->effective_time!=0){
                        $strt_time = strtotime($order_info->effective_time);
                        if ($strt_time<time()){
                            $effective_time_status = 0;
                        }
                    }
                    if ($effective_time_status==1){
                        Db::table("shop_order")->where(["order_no"=>$order_no])->update(
                            [
                                "cancel_status"=>1,
                                "cancel_datetime"=>date("Y-m-d H:i:s"),
                                "mchant_id"=>$mchant_info->id,
                                "mchant_mobile"=>$mchant_info->mobile,
                                "mchant_name"=>$mchant_info->username,
                                "mchant_pid"=>$mchant_info->mt_id,
                            ]
                        );
                        return [
                            "code"=>10000,
                            "message"=>"核销成功",
                            "content"=>(object)[]
                        ];
                    }else{
                        return [
                            "code"=>20066,
                            "message"=>"该卡劵已经过期",
                            "content"=>(object)[]
                        ];
                    }
                }else{
                    if ($order_info->cancel_status==1){
                        return [
                            "code"=>20065,
                            "message"=>"该卡劵已经核销",
                            "content"=>(object)[]
                        ];
                    }else{
                        return [
                            "code"=>20066,
                            "message"=>"该卡劵已过期",
                            "content"=>(object)[]
                        ];
                    }
                }
            }
        }else{
            return [
                "code"=>20064,
                "message"=>"账号不存在！",
                "content"=>(object)[],
            ];
        }


    }









}